package fr.maistre.champagne;

import fr.maistre.SystemeSauvegarde.SaveGameManager;
import fr.maistre.champagne.model.JoueurChampagne;
import fr.maistre.champagne.model.NiveauDeDifficulté;
import fr.maistre.champagne.model.Partie;
import android.content.Intent;
import android.support.v13.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class GameActivity extends FragmentActivity implements PartieHolder {
	
	TextView textPlayerName;
	TextView textCompanyName;
	TextView textDate;
	TextView textFlouz;
	
	SaveGameManager saveManager ;
	
	private Partie partie;

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a {@link FragmentPagerAdapter}
	 * derivative, which will keep every loaded fragment in memory. If this
	 * becomes too memory intensive, it may be best to switch to a
	 * {@link android.support.v13.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game);
		saveManager= new SaveGameManager(this.getApplicationContext());

		this.textPlayerName = (TextView) findViewById(R.id.textViewPlayerName);
		this.textCompanyName = (TextView) findViewById(R.id.textViewCompanyName);
		this.textDate = (TextView) findViewById(R.id.textViewDate);
		this.textFlouz = (TextView) findViewById(R.id.textViewFlouz);

		// Create the adapter that will return a fragment for each of the three
		// primary sections of the activity.
		mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(),
				this);

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

		Intent intent = getIntent();
		if (intent.getExtras().getBoolean("NewGame")) {

			JoueurChampagne joueur = new JoueurChampagne(10000, intent.getStringExtra("PlayerName"), intent.getExtras()
					.getString("CompanyName"));
			partie = new Partie(joueur, NiveauDeDifficulté.NORMAL);
			saveManager.sauvegarder(partie);
	
		} else {
			partie = (Partie)saveManager.load();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.game, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.textFlouz.setText(Integer.toString(partie.getJoueur().getArgent()));
		this.textCompanyName.setText(partie.getJoueur().getNomDeLaCompanie());
		this.textPlayerName.setText(partie.getJoueur().getName());
		this.textDate.setText(Integer.toString(partie.getTourActuel().getDate().getMonth()));
	}

	@Override
	public Partie getPartie() {
		return this.partie;
	}

}
